package sk.stu.fei.tomsteam;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

public class AgentsMonitoringPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private final int INTERVAL = 5000;
	private JLabel synchrInfo;
	private Main mainWindow;
	private JButton startButton;
	private JButton stopButton;
	private JPanel buttonPanel;
	private JTable table;	//add ScrollPane
	Timer timer;
	ImageIcon okIcon;
	ImageIcon nokIcon;
	//private List<JLabel> listOfLabels;
	
	public AgentsMonitoringPanel(Main mainWindow) {
		this.mainWindow = mainWindow;
		initUI();
		
		//listOfLabels = new LinkedList<JLabel>();
		//for(int i = 0; i < 5; i++) {
		//	listOfLabels.add(new JLabel());
		//}
	}
	
	public final void initUI() {
		this.setLayout(new BorderLayout());
		
		synchrInfo = new JLabel();
		synchrInfo.setText("Pooling: Off");
		buttonPanel = new JPanel();
		table = new JTable(new ImageTableModel());
		table.setShowGrid(false);
		TableColumnModel tcm = table.getTableHeader().getColumnModel();
		tcm.getColumn(0).setHeaderValue("Status");
		tcm.getColumn(1).setHeaderValue("Name");
		tcm.getColumn(2).setHeaderValue("Uptime");
		
		okIcon = createImageIcon("images/Ok.png", "OK");
		nokIcon = createImageIcon("images/Nok.png", "NOK");


		
		/*cez startbutton zacat prehladavanie static listu agentov
		 * a volat vypisovaciu metodu kazdeho agenta. List bude naplneny
		 * vsetkymi pridanymi agentami, naplnanie listu sa bude vyko
		 * navat v objekte triedy AddAgentDialog po kliknuti na
		 * saveButton a vytvoreni instancie triedy Agent*/
		
		startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				timer.start();
				synchrInfo.setText("Pooling: On");
				//table.setShowGrid(true);
			}
		});
		
		stopButton = new JButton("Stop");
		stopButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				timer.stop();
				synchrInfo.setText("Pooling: Off");
				//table.setShowGrid(false);
			}
		});
		
		buttonPanel.add(startButton);
		buttonPanel.add(stopButton);
		buttonPanel.add(synchrInfo);
		add(table.getTableHeader(), BorderLayout.PAGE_START);
		add(buttonPanel, BorderLayout.PAGE_END);
		add(table, BorderLayout.CENTER);
		
		ActionListener timerActionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				boolean status;
				String sysUptime;
				
				if(Agent.listOfAgents.isEmpty()) {
					table.setValueAt("Agent list is empty!", 0, 1);
				}
				
				else {
					try{
						for(int i=0; i < Agent.listOfAgents.size(); i++) {
							sysUptime = Agent.listOfAgents.get(i).executorMethod();
							
							if (sysUptime.equals("TimedOut")) {
								status = false;
							}
							else {
								status = true;
							}
							
							if (status) {
								table.setValueAt(okIcon, i, 0);
							}
							else {
								table.setValueAt(nokIcon, i, 0);
							}
							table.setValueAt(Agent.listOfAgents.get(i).getName(), i, 1);
							table.setValueAt(sysUptime, i, 2);
						}
					}
					catch(NullPointerException npe) {
						stopButton.doClick(1500);
						System.out.println("NullPointerExc");
						JOptionPane.showMessageDialog(null, "NullPointerExc", "Error", JOptionPane.ERROR_MESSAGE);
						//timer.stop();
					}
					catch(ArrayIndexOutOfBoundsException e) {
						stopButton.doClick(1500);
						System.out.println("OutOfBoundsExc");
						JOptionPane.showMessageDialog(null, "OutOfBoundsExc", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}				
				
			}
		};
		
		//javax.swing.Timer uses event dispatch thread internally
		timer = new Timer(INTERVAL, timerActionListener);
		
	}
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	private ImageIcon createImageIcon(String path, String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}
}
